/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.SimcardDao; 
import com.tesam.gedeon.entity.Simcard;
import com.tesam.gedeon.service.SimcardService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Sistema
 */
@Service
public class SimcardServiceImpl implements SimcardService {

    @Autowired
    SimcardDao simcardDao ;
    
    @Override
    public int getCountAll() {
        return simcardDao.getCountAll();
    }

    @Override
    public int getCountBySearch(Simcard simcard) {
        return simcardDao.getCountBySearch(simcard);
    }
    
    @Override
    public int getCountByIccid(Simcard simcard){
        return simcardDao.getCountByIccid(simcard);
    }

    @Override
    public List<Simcard> getSimcardByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
        return simcardDao.getSimcardByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }

    @Override
    public List<Simcard> getSimcardBySearch(int startLimit, int endLimit, String dataToSort, String directionOfOrder, Simcard simcard) {
        return simcardDao.getSimcardBySearch(startLimit, endLimit, dataToSort, directionOfOrder, simcard);
    }

    @Override
    public List<Simcard> getSimcardById(Simcard simcard) {
        return simcardDao.getSimcardById(simcard);
    }

    @Override
    public void delete(Simcard simcard) {
         simcardDao.delete(simcard);
    }

    @Override
    public void update(Simcard simcard) {
        simcardDao.update(simcard);
    }

    @Override
    public int save(Simcard simcard) {
        return simcardDao.save(simcard);
    }
    
    @Override
    public String getJsonDataFromArrayList(List<Simcard> listSimcard, int countRecord)
    {
    JSONArray array = null;
        JSONObject json = null;
        String deleteButton = "" ;
        String updateButton = "" ;
        String viewButton = "" ;
        String groupButton = "";
        String providerLink = "";
        String networkLink = "" ;
        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listSimcard.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");
              
                for (Simcard simcard : listSimcard) {
                    JSONArray arrayPut = new JSONArray();
 
                    viewButton = "<a href='view?id="+simcard.getId()+"&module=24' ";
                    viewButton+= "class='blue'><i class='icon-share'></i> View </a>";
                    updateButton = "<a href='update?id="+simcard.getId()+"&module=24' class='green' >" ;
                    updateButton+= "<i class='icon-pencil'></i> Update</a>";
                    
                    deleteButton = "<a class='red' data-toggle='modal' ";
                    deleteButton+= "onclick=\"";     
                    deleteButton+="confirmDelete('"+simcard.getId()+"')";
                    deleteButton+="\" "; 
                    deleteButton+="href='#static' >";
                    deleteButton+="<i class='icon-trash'></i> Delete</a>";  
                    
                    groupButton = "<div class='btn-group'>";
                    groupButton+= "<a class='btn mini purple' href='#' data-toggle='dropdown'>";
                    groupButton+= "Function&nbsp;&nbsp;";
                    groupButton+= "<i class='icon-arrow-down'></i>";
                    groupButton+= "</a>";
                    groupButton+= "<ul class='dropdown-menu'>";
                    groupButton+="<li class='red'>"+viewButton+"</li>";
                    groupButton+="<li>"+updateButton+"</li>";
                    groupButton+="<li>"+deleteButton+"</li>";                      
                    groupButton+= "</ul>";
                    groupButton+= "</div>";  	
                     
                    networkLink = "<a href='../mediationnetwork/view?id="+simcard.getNetwork().getId();
                    networkLink+= "&module="+simcard.getNetwork().getModuleId()+"'>";
                    networkLink+= simcard.getNetwork().getDescription() ;
                    networkLink+= "</a>";
                    
                    providerLink = "<a href='../provider/view?id="+simcard.getProvider().getId();
                    providerLink+= "&module="+simcard.getProvider().getId()+"' >";
                    providerLink+= simcard.getProvider().getName();
                    providerLink+="</a>";
                    
                    arrayPut.put(simcard.getIccid()); 
                    arrayPut.put(simcard.getImsi());  
                    arrayPut.put(simcard.getType());
                    arrayPut.put(simcard.getMovilName());
                    arrayPut.put(simcard.getPlantype());
                    arrayPut.put(providerLink);
                    arrayPut.put(networkLink);
                    
                    arrayPut.put(groupButton);
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", countRecord);
            json.put("iTotalDisplayRecords", countRecord);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }
    
}
